package com.lanou.sfu.songforu.view;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.activity.MainActivity;
import com.lanou.sfu.songforu.adapter.PlayQueueAdapter;
import com.lanou.sfu.songforu.datas.MusicListDatas;

import java.util.ArrayList;


/**
 * Created by quyan on 15/11/3.
 */
public class MusicPopuoWindow extends PopupWindow {
    private ImageView mPlayMode;
    private ListView mListView;
    private TextView clean;
    private View view;
    private PlayQueueAdapter adapter;
    private int typeId;
    private Context context;
    MusicInterface music;

    private static final String modeName[] = {"列表循环", "顺序播放", "随机播放", "单曲循环"};
    private int modeDrawable[] = {R.mipmap.img_appwidget_playmode_repeat,
            R.mipmap.img_appwidget_playmode_sequence, R.mipmap.img_appwidget_playmode_shuffle,
            R.mipmap.img_appwidget_playmode_repeatone};
    public MusicPopuoWindow(MusicInterface music,Context context, ArrayList<MusicListDatas> list) {
        this.context = context;
        this.music = music;
        
        show(list);
    }

    public void show(final ArrayList<MusicListDatas> list) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.playqueue, null);
        mListView = (ListView) view.findViewById(R.id.listview_play_queue);
        clean = (TextView) view.findViewById(R.id.clear_tv);//清除
        mPlayMode = (ImageView) view.findViewById(R.id.playmode_iv);//模式
        mPlayMode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (typeId < 3) {
                    typeId++;
                } else {
                    typeId = 0;
                }
                mPlayMode.setImageResource(modeDrawable[typeId]);
                music.playMode(typeId);
            }
        });
        //清除监听
        clean.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<MusicListDatas> listDatases = new ArrayList<>();
                MainActivity.play(listDatases, 0);
                dismiss();
                MainActivity.clean();
        }
        });
        adapter = new PlayQueueAdapter(context, list);
        mListView.setAdapter(adapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MainActivity.play(list, position);
            }
        });

        //设置PopupWindow显示的view
        this.setContentView(view);
        //设置弹出窗体的高
        this.setHeight(700);
        //设置弹出窗体的宽
        this.setWidth(600);
        //设置窗体可点击
        this.setFocusable(true);
        this.setTouchable(true); //设置PopupWindow可触摸
        this.setOutsideTouchable(true); //设置非PopupWindow区域可触摸

        //设置弹出动画
        this.setAnimationStyle(R.style.AnimationPreview);
        ColorDrawable colorDrawable = new ColorDrawable(0x000000);
        this.setBackgroundDrawable(colorDrawable);
        //        this.setAnimationStyle(android.R.style.Animation_Dialog);
        //添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                int height = view.findViewById(R.id.pop_Layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });
    }
}
